//
//  DateTimeFormat.m
//  MISCloud
//
//  Created by goldeney on 13-10-15.
//  Copyright (c) 2013年 goldeneye. All rights reserved.
//

#import "DateTimeFormat.h"

@implementation DateTimeFormat

#pragma mark -
#pragma mark 返回当前时间，格式20131009000000
+ (NSString*)getNowTime
{
    NSDate *curDate = [NSDate date];//获取当前日期
    NSDateFormatter *formater = [[ NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyyMMddHHmmss"];
    NSString *curTime = [formater stringFromDate:curDate];
    return curTime;
}


+ (NSString*)getNowTimeTypeString:(NSString*)typeString
{
    NSDate *curDate = [NSDate date];//获取当前日期
    NSDateFormatter *formater = [[ NSDateFormatter alloc] init];
    [formater setDateFormat:typeString];
    NSString *curTime = [formater stringFromDate:curDate];
    return curTime;
}

+ (NSString*)getNowDateFormatter:(NSString*)formatter
{
    NSDate *curDate = [NSDate date];//获取当前日期
    NSDateFormatter *formater = [[ NSDateFormatter alloc] init];
    [formater setDateFormat:formatter];
    NSString *curTime = [formater stringFromDate:curDate];
    return curTime;
}

#pragma mark -
#pragma mark 返回当前时间，格式20131009000000000
+ (NSString*)getNowTimeMillisecond
{
    NSDate *curDate = [NSDate date];//获取当前日期
    NSDateFormatter *formater = [[ NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyyMMddHHmmssSSS"];
    NSString *curTime = [formater stringFromDate:curDate];
    return curTime;
}

#pragma mark -
#pragma mark 返回时间格式 20131009000000
+ (NSString*) setNewTimeType:(NSString*)oldTimeTring
{
    NSMutableString *newTimeString = [[NSMutableString alloc] initWithString:oldTimeTring];
    [newTimeString appendString:@" 00:00:00"];
    
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date =[dateFormat dateFromString:newTimeString];
    
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYYMMddHHmmss"];
    NSString * locationString=[dateformatter stringFromDate:date];
    
    return locationString;
}

+ (NSString*) setYearsType:(NSString*)oldTimeTring
{
    NSMutableString *newTimeString = [[NSMutableString alloc] initWithString:oldTimeTring];
    [newTimeString appendString:@"0000000000"];
    
    return newTimeString;
}

#pragma mark -
#pragma mark 返回时间格式 2013-10-09 00:00:00
+ (NSString*) setNormTimeType:(NSString*)oldTimeTring
{
    NSMutableString *newTimeString = [[NSMutableString alloc] initWithString:oldTimeTring];
    [newTimeString appendString:@" 00:00:00"];
    
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date =[dateFormat dateFromString:newTimeString];
    
    return [dateFormat stringFromDate:date];
}

+ (NSString*) setNormYearsType:(NSString*)oldTimeTring
{
    NSMutableString *newTimeString = [[NSMutableString alloc] initWithString:oldTimeTring];
    [newTimeString appendString:@"-00-00 00:00:00"];
    
    return newTimeString;
}


#pragma mark -
#pragma mark 计算某个时间点与现在的时间差

+ (NSString *)intervalSinceNow: (NSString *) theDate
{
    
    NSDateFormatter *date=[[NSDateFormatter alloc] init];
    [date setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    NSDate *d=[date dateFromString:theDate];
    
    NSTimeInterval late=[d timeIntervalSince1970]*1;
    
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval now=[dat timeIntervalSince1970]*1;
    NSString *timeString=@"";
    
    NSTimeInterval cha = late - now;
    
    if (cha/86400>=1) {
        float days = cha/86400 + 1;
        timeString = [NSString stringWithFormat:@"%f", days];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@天后", timeString];
        
    } else if (cha/3600>=1) {
        float hous = cha/3600 + 1;
        timeString = [NSString stringWithFormat:@"%f", hous];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@小时后", timeString];
        
    } else if (cha/60>=1) {
        float minute = cha/60 + 1;
        timeString = [NSString stringWithFormat:@"%f", minute];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@分钟后", timeString];
        
    } else if (cha/60<1 && cha/60>0) {
       timeString=[NSString stringWithFormat:@"1分钟后"];
        
    }else {
        timeString=[NSString stringWithFormat:@"进行中"];
    }
    
    return timeString;
}

#pragma mark -
#pragma mark 比较两个日期的大小

+(int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {
        //NSLog(@"Date1  is in the future");
        return 1;
    }
    else if (result == NSOrderedAscending){
        //NSLog(@"Date1 is in the past");
        return -1;
    }
    //NSLog(@"Both dates are the same");
    return 0;
    
}

@end
